package com.handinfo.game;

import com.handinfo.engine.Actor;
import com.handinfo.engine.Animation;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.util.Tools;

public class GameEffect extends Actor
{
	public static GameEffect s_effect;
	public static final String URL_EFFECT_ANIM = "/anim/effect.anim";

	public static void initStaticResource()
	{
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_ANIM,
				URL_EFFECT_ANIM, 1);
		s_effect = new GameEffect(
				(Animation) (ResourceManager.getInstance().loadResource(
						GameManager.getInstance().m_resource_static, head)));
	}

	public static void disposeStaticResource()
	{
		if (s_effect == null)
			return;
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_ANIM,
				URL_EFFECT_ANIM, 1);
		ResourceManager.getInstance().removeResource(
				GameManager.getInstance().m_resource_static, head);
		s_effect.dispose();
		s_effect = null;
	}

	public AbstractGameActor m_bindActor;
	public int m_curKeyFrame = 0;
	public boolean m_doKeyFrame = false;
	public int m_duration = 0;

	public GameEffect()
	{
		super();
	}

	public GameEffect(Animation anim)
	{
		super(anim);
	}

	public GameEffect(Animation[] anims)
	{
		super(anims);
	}

	public void init(int x, int y, int actionID, boolean doKeyFrame,
			int duration)
	{
		m_duration = duration;
		m_isOnce = (duration == DURATION_NONE);
		m_needDraw = true;
		m_actionCycle = true;
		m_enabled = true;
		m_visible = true;
		m_doKeyFrame = doKeyFrame;
		// test
		m_doKeyFrame = false;

		m_curKeyFrame = 0;
		m_posX = x;
		m_posY = y;
		setAction(actionID, true);
	}

	/**
	 * 克隆给定的元件对象
	 * 
	 * @param actor
	 * @return
	 */
	public Actor clone(Actor actor)
	{
		GameEffect ca = new GameEffect();
		ca.m_scene = actor.m_scene;
		ca.m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, ca.m_anims, 0, actor.m_anims.length);
		ca.m_anim = actor.m_anim;
		ca.m_actionIDNow = actor.m_actionIDNow;
		ca.m_actionIDBefore = actor.m_actionIDBefore;
		ca.m_frameId = actor.m_frameId;
		ca.m_frameIndex = actor.m_frameIndex;
		ca.m_frameDuration = actor.m_frameDuration;
		ca.m_actionCycle = actor.m_actionCycle;
		ca.m_actionOver = actor.m_actionOver;
		ca.m_layer = actor.m_layer;
		ca.m_phylayer = actor.m_phylayer;
		ca.m_flag = actor.m_flag;
		ca.m_flipX = actor.m_flipX;
		ca.m_flipY = actor.m_flipY;
		ca.m_visible = actor.m_visible;
		ca.m_enabled = actor.m_enabled;
		// ca.m_movable = actor.m_movable;
		ca.m_needDraw = actor.m_needDraw;
		ca.m_posX = actor.m_posX;
		ca.m_posY = actor.m_posY;
		ca.m_vX = actor.m_vX;
		ca.m_vY = actor.m_vY;
		ca.m_collideBox = new short[actor.m_collideBox.length];
		System.arraycopy(actor.m_collideBox, 0, ca.m_collideBox, 0, actor.m_collideBox.length);
		ca.m_activeBox = new short[actor.m_activeBox.length];
		System.arraycopy(actor.m_activeBox, 0, ca.m_activeBox, 0, actor.m_activeBox.length);
		ca.m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, ca.m_parameters, 0, actor.m_parameters.length);
		return ca;
	}

	/**
	 * 
	 */
	public void updatePos()
	{
		m_needDraw = (m_posX >= m_scene.m_cameraX)
				&& (m_posX <= m_scene.m_cameraX + m_scene.m_cameraW)
				&& (m_posY >= m_scene.m_cameraY)
				&& (m_posY <= m_scene.m_cameraY + m_scene.m_cameraH);
	}

	/**
	 * 指向动作下一帧
	 */
	public void nextFrame()
	{
		try {
			m_frameDuration++;
			if (m_anim == null
					|| m_frameId >= m_anim.m_actions[m_actionIDNow].length
					|| m_frameDuration < ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10)) {
				return;
			}

			m_frameId++;

			// 关键帧有特殊行为
			if (m_doKeyFrame) {
				if (m_curKeyFrame < KEY_FRAME[m_actionIDNow].length) {
					if (m_frameId >= KEY_FRAME[m_actionIDNow][m_curKeyFrame]) {
						m_curKeyFrame++;
						doKeyFrame();
					}
				}
			}

			if (m_frameId >= m_anim.m_actions[m_actionIDNow].length) {
				m_actionOver = true;
				if (m_actionCycle) {
					m_frameId = 0;
				}
				else {
					m_frameId--;
				}
				// 一次性元件
				if (m_isOnce) {
					m_enabled = false;
				}
			}

			m_frameIndex = (m_anim.m_actions[m_actionIDNow][m_frameId] & 0x03FF);
			m_frameDuration = 0;
		}
		catch (Exception ex) {
			m_enabled = false;
			Tools.debug(ex);
		}
	}

	public void doKeyFrame()
	{
	}

	/**
	 * 获得剩余帧数
	 * 
	 * @return
	 */
	public int getFrameLeft()
	{
		return m_anim.m_actions[m_actionIDNow].length - 1 - m_frameId;
	}

	public static final byte[][] KEY_FRAME = new byte[][] {

	};

	/**
	 * 无持续时间，代表该光效在动画结束后失效
	 */
	public static final int DURATION_NONE = -1;
}
